import { toast } from "./common.js";
import config from "./config.js";

let timeout = 10000;
const baseUrl = config.baseUrl;

const request = (config) => {
  let openId = uni.getStorageSync("openId");
  let urlList = [
    "/api/menu/list",
    "/api/wechat/getOpenId",
    "/api/wechat/userinfo/update",
  ];
  if (!urlList.includes(config.url)) {
    if (!openId) {
      toast({
        title: "请先前往登录~",
        icon: "error",
      });
      return;
    }
  }
  !config.data && (config.data = {});
  config.data.openId = openId;
  return new Promise((resolve, reject) => {
    uni.request({
      method: config.method || "get",
      url: baseUrl + config.url,
      data: config.data,
      success: (response) => {
        if (response.data.code === 0) {
          resolve(response.data);
        } else {
          toast({
            title: response.data.message,
            icon: "error",
          });
          resolve(response);
        }
      },
      error: (err) => {
        console.log(err, 1);
      },
    });
  });
};

export default request;
